/**
 * 数组去重
 * @param arr 去重的数组
 * @return {*}
 */
export const arrayRemoveRepeat = (arr, str = '') => {
  if (Array.isArray(arr)) {
    let newArr = [];
    return arr.filter(item => {
      if (newArr.includes(item[str])) {
        return false;
      } else {
        newArr.push(item[str]);
        return true;
      }
    });
  } else {
    return arr;
  }
};

/**
 * 防抖
 * @param callback 回调函数
 * @param delay 延迟
 * @return {*}
 */
export const debounce = (callback, delay) => {
  let timer = null;

  return function () {
    // 伪数组转数组
    var args = Array.prototype.slice.call(arguments);
    if (timer !== null) {
      clearTimeout(timer);
    }

    timer = setTimeout(() => {
      // 向callback中传递参数, 并且改变当前this的指向, 并且立即调用
      callback.apply(this, args);
    }, delay);
  };
};

/**
 * 节流
 * @param name 描述信息
 * @return {*}
 */
export const throttle = (callback, delay) => {
  var timer = null;

  return function () {
    let arg = Array.prototype.slice.call(arguments);
    if (!timer) {
      timer = setTimeout(() => {
        callback.apply(this, arg);
      }, delay);
    }
  };
};

/**
 * 深拷贝
 * @param name 描述信息
 * @return {*}
 */
export const cloneDeep = obj => {
  const type = typeof obj;
  if (type !== 'object' || obj === null) return obj;

  let clone, len, keys;

  if (Array.isArray(obj)) {
    clone = [];
    len = obj.length;
    for (let i = 0; i < len; i++) {
      clone[i] = cloneDeep(obj[i]);
    }
  } else {
    clone = {};
    keys = Object.keys(obj);
    keys.forEach(key => (clone[key] = cloneDeep(obj[key])));
  }

  return clone;
};

/**
 * 格式化日期格式
 * @param {*} data
 * @param {*} fmt  格式 'yyyy-MM-dd hh:mm:ss'
 * @returns
 */
export function formatDate(data, fmt) {
  function padLeftZero(str) {
    return ('00' + str).substr(str.length);
  }
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (data.getFullYear() + '').substr(4 - RegExp.$1.length));
  }

  let o = {
    'M+': data.getMonth() + 1,
    'd+': data.getDate(),
    'h+': data.getHours(),
    'm+': data.getMinutes(),
    's+': data.getSeconds(),
  };

  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + '';
      fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str));
    }
  }

  return fmt;
}

/**
 * 文件下载
 * @param data 数据
 * @param fileName 文件名
 * @param type 文件类型
 * @return {*}
 */
export function downloadBolbFile(data, fileName, type = '') {
  let blob = new Blob([data], {
    type: `application/${type};charset=utf-8`,
  });
  // 获取heads中的filename文件名
  let downloadElement = document.createElement('a');
  // 创建下载的链接
  let href = window.URL.createObjectURL(blob);
  downloadElement.href = href;
  // 下载后文件名
  downloadElement.download = fileName;
  document.body.appendChild(downloadElement);
  // 点击下载
  downloadElement.click();
  // 下载完成移除元素
  document.body.removeChild(downloadElement);
  // 释放掉blob对象
  window.URL.revokeObjectURL(href);
}

//给日期加0
function addDate0(time) {
  if (time.toString().length == 1) {
    time = '0' + time.toString();
  }
  return time;
}
// 获取近7天日期
export function getSevenDays() {
  let days = [];
  for(let i=0; i<=24*6;i+=24){		
    let dateItem=new Date(new Date().getTime() - i * 60 * 60 * 1000);	
    let y = dateItem.getFullYear();	
    let m = dateItem.getMonth() + 1;	
    let d= dateItem.getDate();	
    m = addDate0(m);
    d = addDate0(d);	
    let valueItem= y + '-' + m + '-' + d;	
    days.push(valueItem);	
  }
  return days.reverse();		
}

// 获取[n,m]范围内的随机数
export function getRangeRandom(n,m){
  const data = Math.floor(Math.random() * (m - n + 1)) + n;
  return data;
}

// 获取[n,m]范围内的随机数 (浮点值)
export function getRangeRandomFloor(n,m, num){
  let Rtx = /^\d+(?:\.\d{0,1})?/;
  if(num == 2) Rtx =  /^\d+(?:\.\d{0,2})?/;
  return Number((Math.random() * (m - n) + n).toString().match(Rtx));
}


